#include <sys/select.h>

#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>

#define BUF_SIZE 30

int main(int argc, char *argv[])
{

    fd_set reads;
    fd_set temp;

    int result;
    int str_len;

    char buf[BUF_SIZE];
    struct timeval tv;

    FD_ZERO(&reads);
    FD_SET(0, &reads);


    while(1)
    {
        temp = reads;
        tv.tv_sec = 5;
        tv.tv_usec = 0;

        result = select( 1, &reads, NULL, NULL, &tv);
        if (-1 == result) {
            puts("select error!");
            break;
        }
        else if ( 0 == result) {
            puts("time out !");

        }
        else {
            if (FD_ISSET(0, &temp)) {
                str_len = read( 0, buf, BUF_SIZE);
                buf[str_len] = '\0';
                printf("message from console: %s\n", buf);
            }

        }

    }


    return 0;
}